package com.geek.javase.ch16.work.work1;

/*
 *@author wangpeng
 *2025/11/2
 *@description TODO
 */

import java.util.concurrent.LinkedBlockingQueue;

public class LinkedBlockingQueueTest  {
    public static void main(String[] args) throws InterruptedException {
        // 创建阻塞队列，用于线程间通信
        LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();

        // 线程1 - 打印A和C
        Thread thread1 = new Thread(() -> {
            try {
                System.out.println("A");

                // 从队列中获取信号，阻塞等待直到收到信号
                String signal = queue.take();

                // 收到信号后打印C
                System.out.println("C");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        // 线程2 - 打印B
        Thread thread2 = new Thread(() -> {
            try {
                Thread.sleep(100); // 确保线程1先打印A
                System.out.println("B");

                // 向队列发送信号，通知线程1可以继续执行
                queue.put("CONTINUE");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        thread1.start();
        thread2.start();
    }
}